local juqi = fk.CreateSkill{
  name = "hanqing__juqi2",
  tags = { Skill.Limited },
}

Fk:loadTranslationTable{
  ["hanqing__juqi2"] = "居奇？",
  [":hanqing__juqi2"] = "限定技，当一名角色的手牌数成为全场最大时，你可以对其发动〖拥嫡〗，然后获得〖吕史〗。",

  ["#hanqing__juqi2-invoke"] = "居奇：你可以对 %dest 发动“拥嫡”，你获得“吕史”",
  ["#hanqing__juqi2-choose"] = "居奇：你可以对其中一名角色发动“拥嫡”，你获得“吕史”",
}

juqi:addEffect(fk.AfterCardsMove, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(juqi.name) and player:usedSkillTimes(juqi.name, Player.HistoryGame) == 0 then
      for _, move in ipairs(data) do
        if move.to and move.toArea == Card.PlayerHand and not move.to.dead and
          table.every(player.room.alive_players, function (p)
            return move.to:getHandcardNum() >= p:getHandcardNum()
          end) then
          return true
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = {}
    for _, move in ipairs(data) do
      if move.to and move.toArea == Card.PlayerHand and not move.to.dead and
        table.every(room.alive_players, function (p)
          return move.to:getHandcardNum() >= p:getHandcardNum()
        end) then
        table.insertIfNeed(targets, move.to)
      end
    end
    if #targets == 1 then
      if room:askToSkillInvoke(player, {
        skill_name = juqi.name,
        prompt = "#hanqing__juqi2-invoke::"..targets[1].id,
      }) then
        event:setCostData(self, {tos = targets})
        return true
      end
    elseif #targets > 1 then
      local to = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        targets = targets,
        skill_name = juqi.name,
        prompt = "#hanqing__juqi2-choose",
        cancelable = true,
      })
      if #to > 0 then
        event:setCostData(self, {tos = to})
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local skill = Fk.skills["hanqing__yongdi"]
    player:broadcastSkillInvoke("hanqing__yongdi")
    room:notifySkillInvoked(player, "hanqing__yongdi")
    local new_event = fk.Damaged:new(room, player, {})
    new_event:setCostData(skill, {tos = {to}})
    skill:use(new_event, player, player, {})
    if player.dead then return end
    room:handleAddLoseSkills(player, "hanqing__lvshi")
  end,
})

return juqi
